<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript">
        var postMessage = function(value) {
            window.webkit.messageHandlers.captcha.postMessage({ message: value });
        }

        var postReCaptchaToken = function(value) {
            window.webkit.messageHandlers.captcha.postMessage({ recaptcha_token: value });
        }

        var postHCaptchaToken = function(value) {
            window.webkit.messageHandlers.captcha.postMessage({ hcaptcha_token: value });
        }

        var gReCaptchaExecute = function() {
            var observer = new MutationObserver(function(mutations) { postMessage("challenge_change"); });
            observer.observe(document.getElementsByTagName("div")[4], { attributes: true, attributeFilter: ['style'] });
            grecaptcha.execute();
        };

        var onReCaptchaLoad = function() {
            var observer = new MutationObserver(function(mutations) {
                var inserted = false;
                for (var i = 0; i < mutations.length; i++) {
                    if (mutations[i].addedNodes[0].nodeName.toUpperCase() == 'div'.toUpperCase()) {
                        inserted = true;
                        break;
                    }
                }
                if (inserted) {
                    observer.disconnect();
                    postMessage("recaptcha_did_load");
                }
            })
            observer.observe(document.getElementsByTagName("body")[0], { childList: true })
            grecaptcha.render('html_element', {
                'sitekey': '${api_key}',
                'callback': postReCaptchaToken,
                'size': 'invisible'
            });
        };

        var onHCaptchaFailed = function() {
            postMessage("hcaptcha_failed");
        }
        
        var onHCaptchaLoad = function() {
            var widgetID = hcaptcha.render('html_element', {
                'sitekey': '${api_key}',
                'callback': postHCaptchaToken,
                'open-callback': function() { postMessage("challenge_change"); },
                'expired-callback': onHCaptchaFailed,
                'chalexpired-callback': onHCaptchaFailed,
                'error-callback': onHCaptchaFailed,
                'size': 'invisible'
            });
            hcaptcha.execute(widgetID);
        }
        </script>
    </head>
    <body>
        <form action="?" method="POST" style="hidden">
            <div id="html_element"></div>
        </form>
        <script src="${script_url}" async defer>
        </script>
    </body>
</html>
